function varargout = Create_New_NRN_Model(varargin)
% CREATE_NEW_NRN_PANEL M-file for create_new_nrn_panel.fig
%      CREATE_NEW_NRN_PANEL, by itself, creates a new CREATE_NEW_NRN_PANEL or raises the existing
%      singleton*.
%
%      H = CREATE_NEW_NRN_PANEL returns the handle to a new CREATE_NEW_NRN_PANEL or the handle to
%      the existing singleton*.
%
%      CREATE_NEW_NRN_PANEL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CREATE_NEW_NRN_PANEL.M with the given input arguments.
%
%      CREATE_NEW_NRN_PANEL('Property','Value',...) creates a new CREATE_NEW_NRN_PANEL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before create_new_nrn_panel_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to create_new_nrn_panel_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help create_new_nrn_panel

% Last Modified by GUIDE v2.5 30-Oct-2007 15:39:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Create_New_NRN_Model_OpeningFcn, ...
    'gui_OutputFcn',  @Create_New_NRN_Model_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before create_new_nrn_panel is made visible.
function Create_New_NRN_Model_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to create_new_nrn_panel (see VARARGIN)

% Choose default command line output for create_new_nrn_panel
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes create_new_nrn_panel wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Create_New_NRN_Model_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in load_data.
function load_data_Callback(hObject, eventdata, handles)
h=findobj('Tag','activity_log');s=get(h,'String');
if ischar(s)
    s={s};set(h,'String',s);
end
[filename,directory]=uigetfile('*','Select tab-delimited text file with data');
%c=clock;c=[,'[',num2str(c(4)),':',num2str(c(5)),':',num2str(round(c(6))),']'];
try
    dt=create_dt([directory,filename]);  % read data file and assemble data structure
    s=[s(1);['> ',datestr(now)];['   data loaded from file'];['   ',directory,filename];['--------------- New Data [',filename,'] ---------------'];' ';s(2:end)];
    set(findobj('Tag','load_data'),'UserData',dt); %store data structure in graphic object
    set(h,'String',s); % update Activity log
    set(findobj('Tag','new_NRN_progress_text'),'String',['data loaded from "',filename,'"'],'ForegroundColor',[0 0.25 0]);
    set(findobj('Tag','explain_datafile_format'),'Visible','off');
    set(findobj('Tag','data_normalization'),'Visible','on');
    set(findobj('Tag','no_normalization'),'Value',1);
    set(findobj('Tag','train_model'),'Visible','on');
    set(findobj('Tag','save_model'),'Visible','off');
    set(findobj('Tag','save_model_s3db'),'Visible','off');
    set(findobj('Tag','stand_alone_fig'),'Visible','on');
    set(findobj('Tag','pca_variables'),'Visible','on');
    set(findobj('Tag','pca_samples'),'Visible','on');
    set(findobj('Tag','cover_colors'),'Visible','off');
    double_cluster_dt_for_GUI(dt);
    %disp(':-)')
catch
    set(findobj('Tag','new_NRN_progress_text'),'String','ERROR LOADING DATA FROM FILE !','ForegroundColor','red');
    s=[s(1);['> ',datestr(now)];[' ERROR LOADING DATA FROM FILE !'];['   ',directory,filename];' ';s(2:end)];
    set(h,'String',s); % update Activity log
    set(findobj('Tag','data_normalization'),'Visible','off')
    %set(findobj('Tag','back_to_main_menu'),'Visible','off');
    set(findobj('Tag','explain_datafile_format'),'Visible','on','ForegroundColor','red')
end
% load data


% hObject    handle to load_data (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on selection change in activity_log.
function activity_log_Callback(hObject, eventdata, handles)
% hObject    handle to activity_log (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns activity_log contents as cell array
%        contents{get(hObject,'Value')} returns selected item from activity_log


% --- Executes during object creation, after setting all properties.
function activity_log_CreateFcn(hObject, eventdata, handles)
% hObject    handle to activity_log (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end






% --- Executes on button press in train_model.
function train_model_Callback(hObject, eventdata, handles)

% update Activity log
h_log=findobj('Tag','activity_log');s=get(h_log,'String');
s=[s(1);'';['> ',datestr(now)];['   Start training NRN model'];s(2:end)];set(h_log,'String',s);
tic_=now;

% train NRN model
h_data=findobj('Tag','load_data');
dt=get(h_data,'UserData');
dt=var_select_neighbor_val_surv(dt,30); % <<< note the maximum number of variables is set here
% update Activity log
h_log=findobj('Tag','activity_log');s=get(h_log,'String');
toc_=now-tic_;
s=[s(1);'';['> ',datestr(now)];['   End training NRN model (',num2str(floor(toc_*24)),':',num2str(floor(toc_*60*24)),':',num2str(floor(toc_*3600*24)),' elapsed)'];s(2:end)];set(h_log,'String',s);
% append log to model
log=get(findobj('Tag','activity_log'),'String');
Ind=strmatch('   data loaded from file',log);
dt.log=log(Ind(1)+2:-1:2);
%put model away
h_data=findobj('Tag','save_model');
set(h_data,'UserData',dt,'Visible','on');
h=guihandles(get(hObject,'Parent'));
set(h.save_model_s3db,'Visible','on');


% hObject    handle to train_model (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function double_cluster_CreateFcn(hObject, eventdata, handles)
text(0.2,0.5,'[no data loaded yet]','Color','red');axis off
% hObject    handle to double_cluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate double_cluster




% --- Executes on button press in no_normalization.
function no_normalization_Callback(hObject, eventdata, handles)
h_data=findobj('Tag','load_data');
dt=get(h_data,'UserData');
dt.nx=dt.x;
set(h_data,'UserData',dt);
double_cluster_dt_for_GUI(dt);
set(findobj('Tag','save_model'),'Visible','off');

% update Activity log
h_log=findobj(gcf,'Tag','activity_log');s=get(h_log,'String');
%c=clock;c=[,'[',num2str(c(4)),':',num2str(c(5)),':',num2str(round(c(6))),']'];
s=[s(1);'';['> ',datestr(now)];['   Normalization reset (raw data retrived)'];s(2:end)];set(h_log,'String',s);

% hObject    handle to no_normalization (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of no_normalization




% --- Executes on button press in quantile_samples.
function quantile_samples_Callback(hObject, eventdata, handles)
h_data=findobj('Tag','load_data');
dt=get(h_data,'UserData');
for i=1:length(dt.nx(1,:))
    if length(unique(dt.nx(:,i)))==1;
        dt.nx(:,i)=0.5*ones(length(dt.nx(:,i)),1);
    else
        dt.nx(:,i)=memb(dt.nx(:,i));
    end
end
set(h_data,'UserData',dt);
double_cluster_dt_for_GUI(dt);
set(findobj('Tag','save_model'),'Visible','off');

% update Activity log
h_log=findobj(gcf,'Tag','activity_log');s=get(h_log,'String');
%c=clock;c=[,'[',num2str(c(4)),':',num2str(c(5)),':',num2str(round(c(6))),']'];
s=[s(1);'';['> ',datestr(now)];['   Quantile normalization across samples'];s(2:end)];set(h_log,'String',s);

% hObject    handle to quantile_samples (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of quantile_samples




% --- Executes on button press in quantile_variables.
function quantile_variables_Callback(hObject, eventdata, handles)
h_data=findobj('Tag','load_data');
dt=get(h_data,'UserData');
for i=1:length(dt.nx(:,1))
    dt.nx(i,:)=memb(dt.nx(i,:)')';
end
set(h_data,'UserData',dt);
double_cluster_dt_for_GUI(dt);
set(findobj('Tag','save_model'),'Visible','off');

% update Activity log
h_log=findobj(gcf,'Tag','activity_log');s=get(h_log,'String');
s=[s(1);'';['> ',datestr(now)];['   Quantile normalization across variables'];s(2:end)];set(h_log,'String',s);

% hObject    handle to quantile_variables (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of quantile_variables




% --- Executes on button press in save_model.
function save_model_Callback(hObject, eventdata, handles)

dt=get(findobj('Tag','save_model'),'UserData');
[filename,directory]=uiputfile('*.nrn','Saving Nearest Ranked Neighbor (NRN) model');
if filename~=0
    save([directory,filename],'dt');
    h_log=findobj(gcf,'Tag','activity_log');s=get(h_log,'String');
    set(findobj('Tag','new_NRN_progress_text'),'String',['model ''''',filename,'" saved'],'ForegroundColor',[0 0.25 0]);
    s=[s(1);'';['> ',datestr(now)];['   Model saved:'];[directory,filename];s(2:end)];set(h_log,'String',s);
end
% hObject    handle to save_model (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)






% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton6.
function pushbutton6_ButtonDownFcn(hObject, eventdata, handles)% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --------------------------------------------------------------------
function back_to_main_menu_Callback(hObject, eventdata, handles)
%disp(':-)')
h=findobj('Tag','NRNmain');
if isempty(h)
   NRNmain
else
    figure(h)
end
% hObject    handle to back_to_main_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in stand_alone_fig.
function stand_alone_fig_Callback(hObject, eventdata, handles)
%disp(':-)')
set(findobj('Tag','color_map'),'UserData',colormap);
h=double_cluster_solo;
dt=get(findobj('Tag','load_data'),'UserData');
double_cluster_dt_for_GUI(dt,h);
% hObject    handle to stand_alone_fig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in PCA_variables.
function PCA_variables_Callback(hObject, eventdata, handles)
% hObject    handle to PCA_variables (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pca_samples.
function pca_samples_Callback(hObject, eventdata, handles)
assignin('base', 'dt', get(findobj('Tag','load_data'),'UserData'));
NRN_PCA_samples;
% hObject    handle to pca_samples (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in pca_variables.
function pca_variables_Callback(hObject, eventdata, handles)
assignin('base', 'dt', get(findobj('Tag','load_data'),'UserData'));
NRN_PCA_vars;
% hObject    handle to pca_variables (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes during object creation, after setting all properties.
function x_cluster_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x_cluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate x_cluster


% --- Executes during object creation, after setting all properties.
function y_cluster_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y_cluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate y_cluster


% --- Executes on mouse press over axes background.
function double_cluster_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to double_cluster (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in color_gray.
function color_gray_Callback(hObject, eventdata, handles)
set(findobj('Tag','color_blue'),'Enable','on');
set(findobj('Tag','color_green'),'Enable','on');
set(findobj('Tag','color_gray'),'Enable','off');
colormap(1-gray);
double_cluster_dt_for_GUI(get(findobj('Tag','load_data'),'UserData'));
% hObject    handle to color_gray (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in color_blue.
function color_blue_Callback(hObject, eventdata, handles)
set(findobj('Tag','color_blue'),'Enable','off');
set(findobj('Tag','color_green'),'Enable','on');
set(findobj('Tag','color_gray'),'Enable','on');
colormap default
double_cluster_dt_for_GUI(get(findobj('Tag','load_data'),'UserData'));
% hObject    handle to color_blue (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in color_green.
function color_green_Callback(hObject, eventdata, handles)
set(findobj('Tag','color_blue'),'Enable','on');
set(findobj('Tag','color_green'),'Enable','off');
set(findobj('Tag','color_gray'),'Enable','on');
colormap(exp_colormap);
double_cluster_dt_for_GUI(get(findobj('Tag','load_data'),'UserData'));
% hObject    handle to color_green (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in save_model_s3db.
function save_model_s3db_Callback(hObject, eventdata, handles)
% hObject    handle to save_model_s3db (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


